//
//  AreaCodeDataSqlite.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/4.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "AreaCodeDataSqlite.h"

@implementation AreaCodeDBModel

@end

#define TableName @"countrycode"
#define CountryC @"countryC"
#define CountryE @"countryE"
#define AreaCode @"code"

@implementation AreaCodeDataSqlite
{
    sqlite3 *myDB;
}


+(instancetype)shareMyData
{
    static AreaCodeDataSqlite *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

/**
 *  打开数据库
 */
-(void)openDB
{
    NSString *database_path = [self getReadyDataBase:@"AreaCodeDataSqlite"];
    
    if (sqlite3_open([database_path UTF8String], &myDB) != SQLITE_OK) {
        sqlite3_close(myDB);
        NSLog(@"数据库打开失败");
    }
}

-(NSString*)getReadyDataBase:(NSString*)dbName
{
    BOOL success;
    NSFileManager* fileManager=[NSFileManager defaultManager];
    NSError* error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [paths objectAtIndex:0];
    NSString *database_path = [documents stringByAppendingPathComponent:dbName];
    success=[fileManager fileExistsAtPath:database_path];
    if(!success)
    {
        NSString* defaultDBPath=[[NSBundle mainBundle] pathForResource:dbName ofType:@"sqlite"];
        if(defaultDBPath!=nil)
        {
            success=[fileManager copyItemAtPath:defaultDBPath
                                         toPath:database_path
                                          error:&error];
        }
        if(!success)
        {
            NSLog(@"打开备选数据库失败:%@",error);
        }
    }
    return database_path;
}


-(void)closeDB
{
    sqlite3_close(myDB);
}

-(BOOL)execSql:(NSString *)sql
{
    @synchronized(NSObject.class)
    {
        char *err;
        if(sqlite3_exec(myDB, [sql UTF8String], NULL, NULL, &err)!= SQLITE_OK)
        {
            NSLog(@"%s",err);
            return NO;
        }
        return YES;
    }
}

-(sqlite3_stmt *)execQuery:(NSString*)sql
{
    @synchronized(NSObject.class)
    {
        sqlite3_stmt * statement;
        const char *err;
        if (sqlite3_prepare_v2(myDB, [sql UTF8String], -1, &statement,&err) != SQLITE_OK) {
            return nil;
        }
        return statement;
    }
}

-(NSMutableArray*)getAllAreaCode
{
    [self openDB];
    @synchronized(NSObject.class)
    {
        NSMutableArray* result = [[NSMutableArray alloc] init];
        NSString *sql = [NSString stringWithFormat:@"select * from %@",TableName];
        sqlite3_stmt *statement = [self execQuery:sql];
        while(sqlite3_step(statement)==SQLITE_ROW)
        {
            AreaCodeDBModel *areaCodeUse = [[AreaCodeDBModel alloc] init];
            
            char *countryE;
            NSString *countryStringE;
            
            char *countryC;
            NSString *countryStringC;
            
            char *areaCode;
            NSString *areaCodeString;
            
            countryE = (char*)sqlite3_column_text(statement, 1);
            countryStringE = [[NSString alloc]initWithUTF8String:countryE];
            
            countryC = (char*)sqlite3_column_text(statement, 2);
            countryStringC = [[NSString alloc]initWithUTF8String:countryC];
            
            areaCode = (char*)sqlite3_column_text(statement, 3);
            areaCodeString = [[NSString alloc] initWithUTF8String:areaCode];
            
            areaCodeUse.countryC = countryStringC;
            areaCodeUse.countryE = countryStringE;
            areaCodeUse.areaCode = areaCodeString;
            
            [result addObject:areaCodeUse];
        }
        sqlite3_finalize(statement);
        [self closeDB];
        return result;
    }
}


@end
